<template>
  <div>
    <div class="select-user">
      <van-field v-model="users" label="发送对象：" @click="toSelectStudent" disabled is-link/>
    </div>
    <div class="sae-textarea">
      <van-field type="textarea" rows="5" autosize v-model="message">
        <div class="sae-label" slot="label">通知内容：</div>
      </van-field>
    </div>
    <div class="sub">
      <van-button type="info" size="large" @click='send'>发送</van-button>
    </div>
  </div>
</template>

<script>
import api from '@/api'
import { Toast } from 'vant'
export default {
  name: 'send-message',
  data () {
    return {
      users: '',
      usersId: '',
      message: ''
    }
  },
  methods: {
    // 跳转到学生选择页面
    toSelectStudent () {
      sessionStorage.setItem('sendMessage', this.message)
      this.$router.push('/select/student/list')
    },
    // 发送课程通知  teacherMessageCourse
    send () {
      let params = {
        teacher_id: JSON.parse(sessionStorage.getItem('user')).id,
        // teacher_id: '1',
        content: this.message,
        student_id: this.usersId
      }
      console.log(params)
      api.teacherMessageCourse({
        params,
        callback: res => {
          Toast.success('发送成功')
          this.$router.go(-1)
        }
      })
    }
  },
  created () {
    // 判断是不是选择学生之后跳转回来，显示弹窗 -> 回显数据 -> 清除缓存数据
    let students = window.sessionStorage.getItem('studentList')
    if (students) {
      let list = JSON.parse(students)
      let arr = []
      let arrID = []
      list.forEach(item => {
        arr.push(item.name)
        arrID.push(item.id)
      })
      this.users = arr.join('、')
      this.usersId = arrID.join(',')
      window.sessionStorage.removeItem('studentList')
      this.message = sessionStorage.getItem('sendMessage')
    }
  }
}
</script>

<style scoped>
  .sub{
    margin-top: 100px;
    padding: 10px;
  }
</style>
